package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Question;
import com.itheima.pojo.Questionnaire;
import com.itheima.service.VolumeGroupservice;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/volumeGroup")
public class VolumeGroupController {

    @Reference
    private VolumeGroupservice volumeGroupservice;

    @RequestMapping("/addVolumeGroup")
    public Result addVolumeGroup(Integer[] questionIds ,@RequestBody Questionnaire questionnaire){
        try {

            volumeGroupservice.addVolumeGroup(questionIds,questionnaire);
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"未知错误");
        }
    }

    @RequestMapping("/addVolume")
    public Result addVolume(Question question){
        try {
            int i = volumeGroupservice.addVolume(question);
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"未知错误");
        }
    }

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult;
        try {
            pageResult = volumeGroupservice.pageQuery(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询问卷列表失败");
        }
        return new Result(true, "查询问卷列表成功", pageResult);
    }


    @RequestMapping("/del")
    public Result del(Integer id){
        try {
            volumeGroupservice.delById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除问卷失败");
        }
        return new Result(true, "删除问卷成功");
    }

    @RequestMapping("/selectQuestion")
    public Result selectQuestion(){
        try {
            List<Question> questions = volumeGroupservice.selectQuestion();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,questions);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"未知错误");
        }
    }

    @RequestMapping("/UpdateVolume")
    public Result UpdateVolume(Integer[] ids,@RequestBody Questionnaire questionnaire){
        try {
            volumeGroupservice.UpdateVolume(ids, questionnaire);
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"未知错误");
        }
    }

    @RequestMapping("/selectids")
    public Result selectids(Integer id){
        try {
            List<Integer> selectids = volumeGroupservice.selectids(id);
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,selectids);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"未知错误");
        }
    };

    @RequestMapping("/UpdateVolumeGroup")
    public Result UpdateVolumeGroup(@RequestBody  Questionnaire questionnaire){
        try {
            volumeGroupservice.UpdateVolumeGroup(questionnaire);
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"未知错误");
        }
    }




}
